package com.javandad.app.server.servlet;

import java.util.Date;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.javandad.app.client.NetworkService;
import com.javandad.app.server.db.DatabaseManager;
import com.javandad.app.server.db.PMFSingleton;
import com.javandad.app.shared.entity.Membership;
import com.javandad.app.shared.entity.User;
import com.javandad.app.shared.entity.UserGroup;

@SuppressWarnings("serial")
public class NetworkServlet extends RemoteServiceServlet implements NetworkService {

	DatabaseManager dbm = DatabaseManager.getDefaultDatabaseManager(); 
	
	@Override
	public List<User> getFriends(String username) {
		PersistenceManager pm = PMFSingleton.getPM();
		List<User> friends = dbm.getFriends(username, pm);
		pm.close();
		return friends;
	}

	@Override
	public List<UserGroup> getGroups(String username) {
		PersistenceManager pm = PMFSingleton.getPM();
		List<UserGroup> groups = dbm.getGroups(username, pm);
		pm.close();
		return groups;
	}

	@Override
	public List<User> getMembers(String groupURL) {
		PersistenceManager pm = PMFSingleton.getPM();
		UserGroup group = pm.getObjectById(UserGroup.class, groupURL);
		List<User> members = dbm.getMembers(group, pm);
		pm.close();
		System.out.println("Members: "+members);
		return members;
	}

	@Override
	public boolean isMember(String username, String groupURL) {
		PersistenceManager pm = PMFSingleton.getPM();
		User user = dbm.getUser(username, pm);
		UserGroup group = pm.getObjectById(UserGroup.class, groupURL);
		Membership m = user.getGroupMembership(group);
		pm.close();
		return (m != null);
	}

	@Override
	public boolean addMember(String username, String groupURL) {
		PersistenceManager pm = PMFSingleton.getPM();
		HttpSession session = this.getThreadLocalRequest().getSession();
		User user = dbm.getUser((String)session.getAttribute("username"), pm);
		UserGroup group = pm.getObjectById(UserGroup.class, groupURL);
		boolean added = addMember(user, group, pm);
		pm.close();
		return added;
	}
	
	private boolean addMember(User user, UserGroup group, PersistenceManager pm) {
		System.out.println("addMember "+user+" "+group);
		Membership m = user.getGroupMembership(group); 
		if(m != null)
			return false;
		m = new Membership(user, group.getGroupURL(), new Date());
		pm.makePersistent(m);
		return true;
	}
	
	@Override
	public boolean removeMember(String username, String groupURL) {
		PersistenceManager pm = PMFSingleton.getPM();
		HttpSession session = this.getThreadLocalRequest().getSession();
		User user = dbm.getUser((String)session.getAttribute("username"), pm);
		UserGroup group = pm.getObjectById(UserGroup.class, groupURL);
		Membership m = user.getGroupMembership(group);//dbm.getMembership(username, groupURL, pm);
		
		if(m != null){
		//	Membership tm = pm.getObjectById(Membership.class, m.getId());
			pm.deletePersistent(m);
		}
		pm.close();
		return true;
	}
	
	@Override
	public boolean createOrJoinGroup(String username, String groupURL) {
		
		PersistenceManager pm = PMFSingleton.getPM();
		HttpSession session = this.getThreadLocalRequest().getSession();
		User user = dbm.getUser((String)session.getAttribute("username"), pm);
		boolean created = false;
		UserGroup group = null;
		try
		{
			group = pm.getObjectById(UserGroup.class, groupURL);
		}
		catch(JDOObjectNotFoundException e)
		{
			group = new UserGroup(groupURL, new Date(), user.getUsername());
			pm.makePersistent(group);
			created = true;
		}
		addMember(user, group, pm);
		pm.close();
		return created;
	}

}
